<template>
	<view class="content">
		<!-- 自定义头部 -->
		<template v-if="list.length > 0">
			<UniNavBar title="购物车" fixed="true" status-bar="true">
				<view slot="right" @click="handleSelect">{{isEdit?'完成':'编辑'}}</view>
			</UniNavBar>
			<!-- 商品内容 -->
			<view class="shop-list">
				<uniSwipeAction>
					<view style="margin-bottom: 15rpx;" v-for="(item, index) in list" :key="index">
						<uniSwipeActionItem :rightOptions="options" @tap="(o) => handleDelete(o, index, item)">
							<view class="shop-item">
								<label class="radio" @tap="selectItem(index)">
									<radio :checked="item.checked"></radio>
								</label>
								<!-- <img class="shop-img" :src="(item.dtPics || '')[0] || $imgErr" /> -->
								<img class="shop-img" :src="item.dtPics" />
								<view class="shop-text">
									<view class="shop-name">{{item.productName}}</view>
									<view class="shop-describe">规格：{{item.productType}}</view>
									<view class="shop-describe">商家：{{item.storeName}}</view>
									<view class="price-number">
										<view class="price">
											￥ {{item.unitPrice || "--"}}
										</view>
										<view class="number">
											<template v-if="isEdit">
												<uniNumberBox :value="item.buyCount" :min="1" size="34rpx" @change="handleNunberChange($event, index)"></uniNumberBox>
											</template>
											<template v-else>
												x {{item.buyCount}}
											</template>
										</view>
									</view>
								</view>
							</view>
						</uniSwipeActionItem>
					</view>
				</uniSwipeAction>
			</view>
			<view class="shop-foot">
				<view class="foot-radio" @tap="handleTapAll">
					<radio :checked="checkedAll"></radio><text>全选</text>
				</view>
				<view class="foot-total">
					<template v-if="isEdit">
						<view class="foot-num" style="background-color: #000000;">
							加入收藏
						</view>
						<view class="foot-num" @click="delGoods">
							删除
						</view>
					</template>
					<template v-else>
						<view class="font-count">
							合计：<text style="color: #007AFF;">￥{{totalCount.price}}</text>
							<view class="freight">
								包含运费
							</view>
						</view>
						<view class="foot-num">
							结算（{{totalCount.num}}）
						</view>
					</template>
				</view>
			</view>
		</template>
		<template v-else>
			<UniNavBar title="购物车" fixed="true" status-bar="true"></UniNavBar>
			<view class="shop-empty">
				<view>
					<image class="shop-none-img" src="../../static/image/shop_none.png"></image>
					<view class="shop-list-none">
						快去商品中心加购你想要的商品吧~
					</view>
				</view>
			</view>
		</template>
		<TabBar currentPage="shop"></TabBar>
	</view>
</template>

<script>
	import TabBar from '@/pages/common/components/TabBar.vue'
	import UniNavBar from '@/components/uni/uni-nav-bar/uni-nav-bar.vue'
	import Icon from '@/components/uni/uni-icons/uni-icons.vue'
	import uniNumberBox from '@/components/uni/uni-number-box/uni-number-box.vue'
	import uniSwipeActionItem from '@/components/uni/uni-swipe-action-item/uni-swipe-action-item.vue'
	import  uniSwipeAction from '@/components/uni/uni-swipe-action/uni-swipe-action.vue'
	import { mapGetters, mapActions, mapMutations } from 'vuex'
	import { updateShopList } from '@/api/shop.js'
	export default {
		data() {
			return {
				isEdit: false,
				options: [
					{
						text: '删除',
						style: {
							backgroundColor: '#dd524d'
						}
					}
				]
			}
		},
		components: {
			UniNavBar,
			Icon,
			uniNumberBox,
			uniSwipeActionItem,
			uniSwipeAction,
			TabBar
		},
		onShow () {
			this.getShopList({})
		},
		computed: {
			...mapGetters(['list', 'checkedAll', 'selectedList', 'totalCount'])
		},
		methods: {
			...mapActions(['handleTapAll', 'delGoods', 'getShopList']),
			...mapMutations(['selectItem']),
			async handleDelete (o, index, item) {
				console.log(o, item)
				await this.selectItem(index)
				this.delGoods()
			},
			handleSelect() {
				this.isEdit = !this.isEdit
			},
			handleNunberChange (value,index) {
				this.list[index].buyCount = value
				updateShopList(this.list[index]).then(res => {
					// uni.showToast({title: '更新数据成功',icon: 'none'});
				})
			}
		}
	}
</script>

<style scoped>
	.content {
		height: 100%;
	}
	.shop-list {
		padding-bottom: 100rpx;
	}
	.shop-item {
		display: flex;
		width: 100%;
		padding: 20rpx;
		align-items: center;
		justify-content: space-between;
		background: #F7F7F7;
		line-height: 50rpx;
	}
	.shop-text {
		flex: 1;
		padding-left: 20rpx;
		padding-right: 40rpx;
	}
	.price-number {
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 28rpx;
		font-weight: 400;
	}
	.shop-img {
		width: 200rpx;
		height: 200rpx;
	}
	.shop-describe {
		font-size: 24rpx;
		color: #666;
	}
	.shop-foot {
		position: fixed;
		bottom: 120rpx;
		left: 0;
		width: 100%;
		height: 100rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		border-top: 2rpx solid #F7F7F7;
		background-color: #FFFFFF;
	}
	.foot-radio {
		padding-left: 20rpx;
	}
	.shop-name {
		font-size: 28rpx;
		line-height: 40rpx;
	}
	.foot-total {
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.foot-num {
		line-height: 100rpx;
		background-color: #007AFF;
		color: #fff;
		padding: 0 50rpx;
	}
	.font-count {
		font-size: 30rpx;
		padding-right: 20rpx;
		text-align: right;
	}
	.freight {
		font-size: 24rpx;
		line-height: 40rpx;
	}
	.shop-empty {
		position: absolute;
		top: 0;
		left: 0;
		bottom: 0;
		right: 0;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.shop-list-none {
		font-size: 30rpx;
		text-align: center;
		color: #666;
	}
	.shop-none-img {
		width: 100%;
	}
</style>
